<?php

namespace Core\annotationhandlers;

use Core\annotations\DB;
use Core\BeanFactory;
use Core\init\MyDb;

return [
    DB::class => function (\ReflectionProperty $property, $instance, DB $DB) {
        if ($DB->source != "default") {
            $sourceDB = MyDb::class . "_" . $DB->source;
            $bean_db = BeanFactory::getBean($sourceDB);
            if (!$bean_db) {
                $bean_db = clone BeanFactory::getBean(MyDb::class);
                $bean_db->setDbSource($DB->source);
                BeanFactory::setBean($sourceDB, $bean_db);
            }
        } else {
            //必须使用clone 在使用过程中MyDb::class 会进行修改
            $bean_db = clone BeanFactory::getBean(MyDb::class);
            $bean_db->setDbSource($DB->source);
        }
        $property->setAccessible(true);
        $property->setValue($instance, $bean_db);
        return $instance;
    }
];
